package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.FlowType;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.FlowTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**资金变动类型控制层
 * @author 田沛凌
 * @time 2017/8/18 11:29;
 */
@RestController
@RequestMapping(value = "flowType", method = RequestMethod.POST)
public class FlowTypeController extends BaseController{

    private FlowTypeService flowTypeService;
    @Autowired
    public void setFlowTypeService(FlowTypeService flowTypeService) {
        this.flowTypeService = flowTypeService;
    }

    /**
     * 新增资金变动类型
     * @param flowType；
     */
    @RequestMapping("/addFlowType")
    public BaseResponse addFlowType(FlowType flowType){
        if (StringUtils.isNullOrEmpty(flowType.getName())){
            throw new AppException(ErrorMessage.FLOW_TYPE_IS_NOT_NULL);
        }
        List<FlowType> list = flowTypeService.list(flowType);
        if (list.size()>0){
            throw new AppException(ErrorMessage.FLOW_TYPE_IS_EXIT);
        }
        flowTypeService.add(flowType);
        return AppResponse.OK;
    }
    /**
     * 查询资金变动类型
     * @param id 资金变动类型ID
     */
    @RequestMapping("/getFlowType")
    public BaseResponse getFlowType(Long id){
        FlowType flowType = flowTypeService.get(id);
        return new AppResponse(flowType);
    }

    /**
     * 查询资金变动类型(分页)
     * @param flowType；
     * @return 资金变动类型集合
     */
    @RequestMapping("/pageFlowType")
    public BaseResponse pageFlowType(FlowType flowType){
        List<FlowType> list = flowTypeService.list(flowType);
        return new AppResponse(new PageInfo<>(list));
    }

    /**
     * 查询资金变动类型
     * @param flowType；
     * @return 资金变动类型集合
     */
    @RequestMapping("/listFlowType")
    public BaseResponse listFlowType(FlowType flowType){
        List<FlowType> list = flowTypeService.list(flowType);
        return new AppResponse(list);
    }

    /**
     * 删除资金变动类型
     * @param id 资金变动类型ID
     */
    @RequestMapping("/delFlowType")
    public BaseResponse delFlowType(Long id){
        flowTypeService.del(id);
        return AppResponse.OK;
    }

    /**
     * 修改资金变动类型
     * @param flowType；
     */
    @RequestMapping("/editFlowType")
    public BaseResponse editFlowType(FlowType flowType){
        flowTypeService.edit(flowType);
        return AppResponse.OK;
    }
}
